package com.google.codejam.Qua2013;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class TicTacToeTomek {

    public static final String[] STATUS = { "X won", "O won", "Draw", "Game has not completed" };

    /**
     * @param args
     */
    public static void main(String[] args) {
        String inputFile = args[0];
        String outputFile = args[1];
        readAndSolve(inputFile, outputFile);
    }

    public static String readAndSolve(String inFileName, String outFileName) {
        File file = new File(inFileName);
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(file));
            FileWriter fileWriter = new FileWriter(outFileName, true);
            String readLine = null;
            int testCase = 0;
            int currentTestCaseIndex = 1;
            int line = 0;
            // intialize array
            String[][] board = new String[4][];
            for (int i = 0; i < 4; i++) {
                board[i] = new String[4];
            }

            while ((readLine = bufferedReader.readLine()) != null) {
                if (readLine.trim().length() <= 0) {
                    continue;
                }
                if (line == 0) {
                    testCase = Integer.parseInt(readLine);
                } else {
                    int index = line % 4 - 1;
                    if (line % 4 == 0) {
                        index = 3;
                    }
                    // insert
                    for (int i = 0; i < 4; i++) {
                        board[index][i] = String.valueOf(readLine.charAt(i));
                    }

                    if (index == 3) {
                        // begin to solve
                        String winSymbole = getWinSymbol(board);
                        fileWriter.append("Case #" + currentTestCaseIndex + ": ");
                        if (winSymbole.equals("X")) {
                            fileWriter.append(STATUS[0] + "\n");
                        } else if (winSymbole.equals("O")) {
                            fileWriter.append(STATUS[1] + "\n");
                        } else if (winSymbole.equals("T")) {
                            fileWriter.append(STATUS[2] + "\n");
                        } else {
                            fileWriter.append(STATUS[3] + "\n");
                        }
                        currentTestCaseIndex++;
                    }
                }
                line++;
            }
            fileWriter.flush();
            fileWriter.close();
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String getWinSymbol(String[][] board) {
        // rowcheck
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                String symbolForRowCheck = board[i][j];
                if (symbolForRowCheck.equals("X") || symbolForRowCheck.equals("O")) {
                    if (checkWin(board[i], symbolForRowCheck)) {
                        return symbolForRowCheck;
                    }
                    break;
                }
            }
        }
        // column check
        String[] symbolsForCheck = new String[4];
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                symbolsForCheck[j] = board[j][i];
            }
            if (checkWin(symbolsForCheck, "X")) {
                return "X";
            }
            if (checkWin(symbolsForCheck, "O")) {
                return "O";
            }
        }

        // dia check
        for (int i = 0; i < 4; i++) {
            symbolsForCheck[i] = board[i][i];
        }
        if (checkWin(symbolsForCheck, "X")) {
            return "X";
        }
        if (checkWin(symbolsForCheck, "O")) {
            return "O";
        }
        // dia check2
        for (int i = 0; i < 4; i++) {
            symbolsForCheck[i] = board[i][3 - i];
        }
        if (checkWin(symbolsForCheck, "X")) {
            return "X";
        }
        if (checkWin(symbolsForCheck, "O")) {
            return "O";
        }

        // check not complete
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (board[i][j].equals(".")) {
                    return ".";
                }
            }
        }
        // game draw
        return "T";
    }

    // private static void output(String[] board){
    // for(int i=0;i < board.length; i++){
    // System.out.print(board[i]+" ");
    // }
    // System.out.println();
    // }

    private static boolean checkWin(String[] symbols, String symbol) {
        for (int i = 0; i < 4; i++) {
            if (symbols[i].equals(".")) {
                return false;
            }
            if (symbols[i].equals("T")) {
                continue;
            }
            if (!symbols[i].equals(symbol)) {
                return false;
            }
        }
        return true;
    }

}
